package cn.ecut.DBConnection;

import java.sql.*;

public class connecttest {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
                "databaseName=thirdtest;user=mx;password=wanyuwen2000@;";/*注册驱动*/
        Connection coon = DriverManager.getConnection(connectionUrl);/*链接connection*/
        System.out.println("coon="+coon);/*查看参数*/
         Statement stat = coon.createStatement();/*获取statement 发送器*/
        String sql="select* from cutomer";/*sql语句*/
        ResultSet rs = stat.executeQuery(sql);/*result接收*/
        while (rs.next()){/*rs.next()往下移动一行*/
            String cusname = rs.getString("cusname");/*也可写数字代表列索引，从1开始要*/
            String custel = rs.getString("custel");
            String cusemail = rs.getString("cusemail");
            String cuspassword = rs.getString("cuspassword");
            System.out.println(cusname+" "+custel+" "+cusemail+" "+cuspassword+"\n");}
            rs.close();
            stat.close();
            coon.close();
       /*try{ Statement stat=coon.createStatement();
        coon.setAutoCommit(false);
        String sq10="update cutomer set custel='12' where cusname='jack'";
        String sq11="update cutomer set custel='13' where cusname='wan'";
           int i = stat.executeUpdate(sq10);
           int i1 = stat.executeUpdate(sq11);
           if(i==1 && i1==1) {
               coon.commit();
               System.out.printf("更新成功");}
           else{
               coon.rollback();
               System.out.printf("更新失败");}
       }catch (Exception e){
           coon.rollback();
       }
       finally {
           coon.close();
       }*/

     /*  Scanner scan=new Scanner(System.in);
        System.out.printf("请输入要查询的名字：");
        String pscs=scan.nextLine();
        String sql5="select * from cutomer where cusname=?;";
        PreparedStatement ps=coon.prepareStatement(sql5);
        ps.setString(1,pscs);
       ResultSet rs= ps.executeQuery();
       while (rs.next()){
           String  cn=rs.getString(1);
           String ct=rs.getString(2);
           String cm=rs.getString(3);
           String cp=rs.getString(4);
           if(cn.equals(pscs)) System.out.printf(cn+" "+ct+" "+cm+" "+cp);
       }
       rs.close();
       ps.close();
       coon.close();*/
        /*简单登陆模拟
        System.out.println("开始登录");
        Scanner scan=new Scanner(System.in);
        System.out.printf("请输入用户名：\n");
        String cusna=scan.next();
        System.out.printf("请输入密码：\n");
        String cupass=scan.next();
        Statement stat4=coon.createStatement();
       String sql4="select * from cutomer";
       ResultSet rs1=stat4.executeQuery(sql4);
       int flag=0;
       while (rs1.next()){
           String cusname=rs1.getString(1);
           String cuspas=rs1.getString(4);
           if(cusname.equals(cusna)&&cuspas.equals(cupass)){
               System.out.printf("登录成功");//简单登录模拟
               flag=1;
               break;
           }
       }
       if(flag==0)
        System.out.printf("登陆失败！");
        rs1.close();
        stat4.close();
        coon.close();*/

   /*   Statement stat1=coon.createStatement();
        String sql1="INSERT INTO cutomer(cusname,custel,cusemail,cuspassword)values('Mike','14257452174','147@417.com','654')";
        int i=stat1.executeUpdate(sql1);
        System.out.println("成功添加"+i+"行");*//*增加*/
        /*Statement stat2=coon.createStatement();
        String sql2="delete from cutomer where cusname='ikke'";
        int i=stat2.executeUpdate(sql2);
        System.out.println("成功删除"+i+"行");*//*删除*/
        /* Statement stat3=coon.createStatement();
        String sql3="update cutomer set cuspassword='456' where cusname='Mike'";/*大小写无关。*/
       /* int i=stat3.executeUpdate(sql3);
        System.out.println("成功修改"+i+"行");*//*修改*/
/* Statement stat = coon.createStatement();/*获取statement 发送器*/
//        String sql="select* from cutomer";/*sql语句*/
//        ResultSet rs = stat.executeQuery(sql);/*result接收*/
//        while (rs.next()){/*rs.next()往下移动一行*/
//            String cusname = rs.getString("cusname");/*也可写数字代表列索引，从1开始要*/
//            String custel = rs.getString("custel");
//            String cusemail = rs.getString("cusemail");
//            String cuspassword = rs.getString("cuspassword");
//            System.out.println(cusname+" "+custel+" "+cusemail+" "+cuspassword+"\n");
//         rs.close();
//            stat.close();
//            coon.close();}*/



    }
}

